/*
 * 
 *方法的整理与重构
 *
 *
 */

//定义app参数名称，并加载其它模块
var authApp=angular.module('authapp', ["ngCookies",'ngFileUpload','ui.bootstrap']);

//此为定位页面所用
authApp.value('page','expert_connect');

/*controller部分*/

//定义父controller，主要用于controller之间传递信息
authApp.controller("fcontroller",function($scope,$http){

})


//主要操作controller
authApp.controller("chooseSub",function($scope,$http,Upload,$timeout,$sce){
	//针对此页面需要的参数 should edit
	$scope.isShowModel=true;
	$scope.firstPointList = "";
	//$scope.firstPointid = "";
	$scope.schoolListId = "";
	//**********主体方法
	var queryMainApi=apis.queryExpertGroupInfo;
	$scope.queryMain=function(){
		$http({
			method:'get',
			url:queryMainApi,
			params:{},
		}).success(function(response){
			if(response.code=="000000"){
				$scope.queryMainApiCallBack(response);
			}else{
				alert(response.errorMsg);
			}
		}).error(function(){
			alert("网络出现故障，请确认网络是否连接");
		})
	}
	$scope.queryMain();
	
	/*下面为自已编辑部分*/
	
	//调用主接口成功之后的处理方法
	$scope.queryMainApiCallBack=function(response){
		if(response.code=="000000"){
			//console.log(response.data.data);
			$scope.expertList=response.data;
		}else{
			alert(response.errorMsg);
		}
	}
	
	/*
	 * 
	 * 其它操作方法
	 * 
	 */
	//分配任务
	$scope.shareTasks=function(expertId){
		$scope.expertId=expertId;
		$scope.isShowModel=false;
		$scope.firstPointList = "";
		//$scope.firstPointid = "";
		$scope.getAllProject(expertId);
	}
	//取消分配任务
	$scope.hideModel=function(){
		$scope.isShowModel=true;
	}
	//获取所有的测试项目
	$scope.getAllProject=function(expertId){
		$http({
			method:'get',
			url:apis.querySchoolGroupMap,
			params:{},
		}).success(function(response){
			if(response.code=="000000"){
				$scope.schoolLists=response.data;
			}else{
				alert(response.errorMsg);
			}
		}).error(function(){
			alert("网络出现故障，请确认网络是否连接");
		})
	}
	$scope.queryItemLists = function(schoolId){
		$scope.schoolListId = schoolId;
		$http({
			method:'post',
			url:apis.queryExpertItemMap,
			data:{'schoolId':schoolId},
		}).success(function(response){
			if(response.code=="000000"){
				$scope.firstPointList=response.data;
			}else{
				alert(response.errorMsg);
			}
		}).error(function(){
			alert("网络出现故障，请确认网络是否连接");
		})
	}

	//点击checkbox
	$scope.updateSelection = function($event,schoolList){
		$scope.firstPointList = "";
		//$scope.firstPointid = "";
        var checkbox = $event.target ;
        var checked = checkbox.checked ; 
        if(checked){
			schoolList.s_state="1";
			$scope.queryItemLists(schoolList.p_id);
		}else{
			schoolList.s_state="0";
			$scope.firstPointList = ''
			//$scope.firstPointList = "";
        }
    }
	$scope.updateSelection1 = function($event,arraylist){
		var checkbox = $event.target ;
		var checked = checkbox.checked ;
		if(checked){
			//$scope.firstPointid += (arraylist.p_id + ",");
			arraylist.flag="1";
		}else{
			arraylist.flag="0";
		}
	}
	//确认分配任务
	$scope.shareTaskSure=function(){
		var lists = [];
		var listsSchool = []
		var cache = []
		angular.forEach($scope.firstPointList,function(data,index,array){
			if(data.flag == '1'){
				lists.push(data.p_id);
			}
		});
		
		lists = lists.join(',')
		angular.forEach($scope.schoolLists,  function(data, index, array) {
			if (data.s_state == '1') {
				listsSchool.push(data.p_id)
			}
		})
		for (var i = 0; i < listsSchool.length; i++) {
			cache.push(lists)
		}
		lists = cache.join('#')
		listsSchool = listsSchool.join('#')
		//expertId 一级指标id
		//schoolId 学校id
		if(window.confirm("是否确认添加？")) {
			$http({
				method: 'post',
				url: apis.saveExpertItemMap,
				data: {'schoolId': listsSchool, 'expertId': $scope.expertId, 'itemList': lists},
			}).success(function (response) {
				if (response.code == "000000") {
					alert("分配成功");
					$scope.isShowModel = true;
				} else {
					alert(response.errorMsg);
				}
			}).error(function () {
				alert("网络出现故障，请确认网络是否连接");
			})
		}
	}
})